<?php
namespace Course\Controller;
use Common\Controller\AdminbaseController;
class AdminOrderController extends AdminbaseController {

	protected $order_obj;
	function _initialize() {
		parent::_initialize();
		$this->order_obj = D("CourseOrder");

	}
	function index(){
		
		if(isset($_POST['addrid'])){
			$addrid = $_POST['addrid'];
			$addr = M('user_addr')->where("id = '$addrid'")->find();
		}
		$where = "1 = 1";
		if(isset($_POST['submit'])){
			$type = I('type');
			$start_time = strtotime(I('start_time'));
			if(isset($_POST['end_time'])){
				$end_time = strtotime(I('end_time').' 23:59:59');
			}
			$keyword = I('keyword');
			if($type && $type != ''){
				$where .= " and state = '$type'";
			}
			if($start_time){
				$where .= " and addtime >= '$start_time'";
			}
			if($end_time){
				$where .= " and addtime <= '$end_time'";
			}
			if($keyword){
				$where .= " and `order` like '%$keyword%'";
			}
		}
		$count=M('course_order')->where($where)->count();
		$page = $this->page($count, 20);
		$order = M('course_order')->where($where)->order("addtime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach($order as $v){
			if($v['course_id'] != ''){
				$cidarr = explode(',', $v['course_id']);
			}
			if($v['good_id'] != ''){
				$gidarr = explode(',', $v['good_id']);
			}
			$cc = array();
			$gg = array();
			
			foreach($cidarr as $vv){
				$course = M('course')->field("cs_name")->where("id = '$vv'")->find();
				$cc[] = $course['cs_name'];
				
			}
			foreach($gidarr as $vvv){
				$good = M('goods_j')->field("name")->where("id = '$vvv'")->find();
				$gg[] = $good['name'];
			}
			$v['course_id'] = $cc;
			$v['good_id'] = $gg;
			$a[] = $v;
		}
		$this->assign('addr',$addr);
		$this->assign('count',$count);
		$this->assign("page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign('order',$a);
		$this->display();
	}

	public function fahuo(){
		$id = I('id');
		$data['is_spend'] = 1;
		$save = M('course_order')->where("id = '$id'")->save($data);
		if($save){
			$this->success('操作成功！');
		}else{
			$this->error('操作失败！');
		}
	}

	public function get_excet(){
		$xlsCell  = array('手机号','身份证','订单编号','班次','课程','教材','支付状态','下单时间','总价','支付方式','是否发货');
		$getid = $_POST['id'];
		$startime = strtotime($_POST['start_time']);
		if($_POST['end_time'] != ''){
			$endtime = strtotime($_POST['end_time'].' 23:59:59');
		}
//		$state = $_POST['state'];
		if(!$getid || $getid == ''){
			$where = "`state` = '1'";
//			if($state){
//				$where = " and `state` = '$state'";
//			}
			if($startime){
				$where .= " and addtime >= '$startime'";
			}
			if($endtime){
				$where .= " and addtime <= '$endtime'";
			}
			$order = M('course_order')->where($where)->order("addtime desc")->select();
			foreach($order as $v){
				$banstr = '';
				$coursestr = '';
				$goodstr = '';
				$userid = $v['user_id'];
				$user = M('users')->field('mobile')->where("id = '$userid'")->find();//手机号
				$usershow = M('user_show')->field('idcard')->where("uid = '$userid'")->find();//身份证
				$banid = explode(',', $v['class_id']); //班次
				foreach($banid as $bid){
					$banstr .= getcourseclassname($bid).',';
				}
				if(substr($banstr,-1) == ','){
					$banstr = substr($banstr,0,-1);
				}
				$courseid = explode(',', $v['course_id']);//课程
				foreach($courseid as $cid){
					$coursestr .= course($cid).',';
				}
				if(substr($coursestr,-1) == ','){
					$coursestr = substr($coursestr,0,-1);
				}
				$goodid = explode(',', $v['good_id']); //教材
				foreach($goodid as $gid){
					$goodstr .= getgoodname($gid).',';
				}
				if(substr($goodstr,-1) == ','){
					$goodstr = substr($goodstr,0,-1);
				}
				
				$arr['mobile'] = $user['mobile'];
				$arr['idcard'] = $usershow['idcard'];
				$arr['order'] = $v['order'];
				$arr['class'] = $banstr;
				$arr['course'] = $coursestr;
				$arr['good'] = $goodstr;
				$v['state'] == 1?$arr['state'] = '已支付':$arr['state']='未支付';
				$arr['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
				$arr['total'] = $v['total'];
				$v['ordertype'] == 1?$arr['ordertype'] = '微信':$arr['ordertype'] = '支付宝';
				$v['is_spend'] == 1?$arr['is_spend'] = '已发货':$arr['is_spend'] = '未发货';
				$data[] = $arr;
			}
		}else{
			foreach($_POST['id'] as $v){
				$banstr = '';
				$coursestr = '';
				$goodstr = '';
				$order = M('course_order')->where("id = '$v'")->order("addtime desc")->find();
				$userid = $order['user_id'];
				$user = M('users')->field('mobile')->where("id = '$userid'")->find();//手机号
				$usershow = M('user_show')->field('idcard')->where("uid = '$userid'")->find();//身份证
				if($order['class_id'] != ''){
					$banid = explode(',', $order['class_id']); //班次
					foreach($banid as $bid){
						$banstr .= getcourseclassname($bid).',';
					}
					if(substr($banstr,-1) == ','){
						$banstr = substr($banstr,0,-1);
					}
				}
				if($order['course_id'] != ''){
					$courseid = explode(',', $order['course_id']);//课程
					foreach($courseid as $cid){
						$coursestr .= course($cid).',';
					}
					if(substr($coursestr,-1) == ','){
						$coursestr = substr($coursestr,0,-1);
					}
				}
				if($order['good_id'] != ''){
					$goodid = explode(',', $order['good_id']); //教材
					foreach($goodid as $gid){
						$goodstr .= getgoodname($gid).',';
					}
					if(substr($goodstr,-1) == ','){
						$goodstr = substr($goodstr,0,-1);
					}
				}
				$arr['mobile'] = $user['mobile'];
				$arr['idcard'] = $usershow['idcard'];
				$arr['order'] = $order['order'];
				$arr['class'] = $banstr;
				$arr['course'] = $coursestr;
				$arr['good'] = $goodstr;
				$order['state'] == 1?$arr['state'] = '已支付':$arr['state']='未支付';
				$arr['addtime'] = date('Y-m-d H:i:s',$order['addtime']);
				$arr['total'] = $order['total'];
				$order['ordertype'] == 1?$arr['ordertype'] = '微信':$arr['ordertype'] = '支付宝';
				$order['is_spend'] == 1?$arr['is_spend'] = '已发货':$arr['is_spend'] = '未发货';
				$data[] = $arr;
			}
		}
		getexcel($data,$xlsCell,'test');die;
	}
}